﻿using System;
using System.Collections.Generic;
using System.Text;
using AgilePlannerDT.Data;

namespace AgilePlannerDT.Persister.Impl
{
    public abstract class AbstractAsyncPersister : AsynchronousPersister
    {
        #region Events

        public delegate void ArrangedProjectEventHandler(Project project);
        public event ArrangedProjectEventHandler OnArrangedProject;

        public delegate void CreatedBacklogEventHandler(Backlog backlog);
        public event CreatedBacklogEventHandler OnCreatedBacklog;

        public delegate void CreatedIterationEventHandler(Iteration iteration,bool isCreatedByRemoteEvent);
        public event CreatedIterationEventHandler OnCreatedIteration;

        public delegate void CreatedProjectEventHandler(Project project);
        public event CreatedProjectEventHandler OnCreatedProject;

        public delegate void CreatedStoryCardEventHandler(StoryCard storycard);
        public event CreatedStoryCardEventHandler OnCreatedStoryCard;

        public delegate void DeletedIterationEventHandler(Iteration iteration);
        public event DeletedIterationEventHandler OnDeletedIteration;

        public delegate void DeletedBacklogEventHandler(Backlog backlog);
        public event DeletedBacklogEventHandler OnDeletedBacklog;

        public delegate void DeletedProjectEventHandler(bool deleted);
        public event DeletedProjectEventHandler OnDeletedProject;

        public delegate void DeletedStoryCardEventHandler(StoryCard storycard);
        public event DeletedStoryCardEventHandler OnDeletedStoryCard;

        public delegate void GotProjectNamesEventHandler(List<String> projectNames);
        public event  GotProjectNamesEventHandler OnGotProjectNames;

        public delegate void LoadedProjectEventHandler(Project project);
        public event LoadedProjectEventHandler OnLoadedProject;

        public delegate void MovedStoryCardToNewParentEventHandler(StoryCard storycard);
        public event MovedStoryCardToNewParentEventHandler OnMovedStoryCardToNewParent;

        public delegate void UndeletedBacklogEventHandler(Backlog backlog);
        public event UndeletedBacklogEventHandler OnUndeletedBacklog;

        public delegate void UndeletedCardEventHandler(IndexCard card);
        public event UndeletedCardEventHandler OnUndeletedCard;

        public delegate void UndeletedIterationEventHandler(Iteration iteration);
        public event UndeletedIterationEventHandler OnUndeletedIteration;

        public delegate void UndeletedStoryCardEventHandler(StoryCard storyCard);
        public event UndeletedStoryCardEventHandler OnUndeletedStoryCard;

        public delegate void UpdatedBacklogEventHandler(Backlog backlog);
        public event UpdatedBacklogEventHandler OnUpdatedBacklog;

        public delegate void UpdatedIterationEventHandler(Iteration iteration);
        public event UpdatedIterationEventHandler OnUpdatedIteration;

        public delegate void UpdatedLegendEventHandler(Legend legend);
        public event UpdatedLegendEventHandler OnUpdatedLegend;

        public delegate void UpdatedProjectEventHandler(Project project);
        public event UpdatedProjectEventHandler OnUpdatedProject;

        public delegate void UpdatedStoryCardEventHandler(StoryCard storycard);
        public event UpdatedStoryCardEventHandler OnUpdatedStoryCard;

        public delegate void AsynchronousExceptionEventHandler(Exception e);
        public event AsynchronousExceptionEventHandler OnAsynchronousException;

        public delegate void MouseMoveEventHandler(MouseMove move);
        public event MouseMoveEventHandler OnMouseMoved;

        public delegate void DisconnectEventHandler(int clientID);
        public event DisconnectEventHandler OnOtherClientDisconnected;

        public delegate void LiveUpdateEventHandler(LiveUpdate update);
        public event LiveUpdateEventHandler OnLiveUpdated;

        public delegate void MinimizeCardEventHandler(long ID,bool isMinimized);
        public event MinimizeCardEventHandler OnCardMinimized;

        public delegate void StartVotingEventHandler();
        public event StartVotingEventHandler OnStartedVoting;

        public delegate void StopVotingEventHandler();
        public event StopVotingEventHandler OnStoppedVoting;

        public delegate void VoteCardEventHandler(VoteDot dot);
        public event VoteCardEventHandler OnVotedCard;

        public delegate void ClearVotesEventHandler();
        public event ClearVotesEventHandler OnClearedVotes;
        
        #endregion
        
        #region AsynchronousPersister Members

        #region Properties

        public abstract Project Project
        {
            get;
            set;
        }

        #endregion

        #region Connection

        public abstract bool IsConnected();

        public abstract void Connect(string projectName);

        public abstract void Disconnect();

        public abstract void Login(string userName, string password);

        #endregion

        #region Load

        public abstract void Load(string projectName);

        public abstract void Load(string projectName, DateTime start, DateTime end);

        public abstract void GetProjectNames();

        #endregion

        #region Create

        public abstract void CreateBacklog(int width, int height, int locationX, int locationY);

        public abstract void CreateIteration(string name, string description, int width, int height, int locationX, int locationY, float availableEffort, DateTime startDate, DateTime endDate);

        public abstract void CreateProject(string projectName);

        public abstract void CreateStoryCard(string name, string description, int width, int height, int locationX, int locationY, long parentid, float bestCaseEstimate, float mostlikelyEstimate, float worstCaseEstimate, float actualEffort, string status, string color, string cardOwner, float rotationAngle);

        #endregion

        #region Delete

        public abstract void DeleteBacklog(long id);

        public abstract void DeleteIteration(long id);

        public abstract void DeleteProject(string projectName);

        public abstract void DeleteStoryCard(long id);

        #endregion

        #region Undelete

        public abstract void UndeleteIteration(Iteration iteration);

        public abstract void UndeleteStoryCard(StoryCard storycard);

        #endregion

        #region Move

        public abstract void MoveStoryCardToNewParent(StoryCard sc, long newparentid, int locationX, int locationY, float rotationAngle);

        #endregion

        #region Update

        public abstract void UpdateBacklog(Backlog backlog);

        public abstract void UpdateIteration(Iteration iteration);

        public abstract void UpdateLegend(Legend legend);

        public abstract void UpdateStoryCard(StoryCard storycard);

        public abstract void UpdateProject(Project project);

        #endregion

        #region Project

        public abstract void ArrangeProject(Project project);

        #endregion

        #region MouseMove

        public abstract void MouseMove(MouseMove  move);

        #endregion

        #region Minimize Card

        public abstract void MinimizeCard(long ID);

        #endregion

        #region synch with jazz

        public abstract void SynchWithJazz(int MessageType, Dictionary<string, string> data);

        #endregion

        #region Live Update

        public abstract void LiveUpdate(LiveUpdate update);

        #endregion

        #region Dot Voting

        public abstract void StartVoting();
        public abstract void StopVoting();
        public abstract void VoteCard(VoteDot dot);
        public abstract void ClearVotes();

        #endregion

        #region Team Member

        public abstract void AddTeamMember(TeamMember member);

        public abstract void RemoveTeamMember(TeamMember member);

        #endregion

        #region Callback Listeners

        public void AddPlannerDataChangeListener(PlannerDataChangeListener listener)
        {
            OnArrangedProject += listener.OnArrangedProject;

            OnCreatedBacklog += listener.OnCreatedBacklog;
            OnCreatedIteration += listener.OnCreatedIteration;
            OnCreatedProject += listener.OnCreatedProject;
            OnCreatedStoryCard += listener.OnCreatedStoryCard;

            OnDeletedBacklog += listener.OnDeletedBacklog;
            OnDeletedIteration += listener.OnDeletedIteration;
            OnDeletedProject += listener.OnDeletedProject;
            OnDeletedStoryCard += listener.OnDeletedStoryCard;

            OnGotProjectNames += (listener.OnGotProjectNames);
            OnLoadedProject += listener.OnLoadedProject;

            OnMovedStoryCardToNewParent += listener.OnMovedStoryCardToNewParent;

            OnUndeletedBacklog += listener.OnUndeletedBacklog;
            OnUndeletedIteration += listener.OnUndeletedIteration;
            OnUndeletedStoryCard += listener.OnUndeletedStoryCard;

            OnUpdatedBacklog += listener.OnUpdatedBacklog;
            OnUpdatedIteration += listener.OnUpdatedIteration;
            OnUpdatedLegend += listener.OnUpdatedLegend;
            OnUpdatedProject += listener.OnUpdatedProject;
            OnUpdatedStoryCard += listener.OnUpdatedStoryCard;

            OnAsynchronousException += listener.OnAsynchronousException;

            OnMouseMoved += listener.OnMouseMove;


            OnLiveUpdated += listener.OnLiveUpdated;

            OnCardMinimized += listener.OnCardMinimized;

            OnStartedVoting += listener.OnStartedVoting;
            OnStoppedVoting += listener.OnStoppedVoting;
            OnVotedCard += listener.OnVotedCard;
            OnClearedVotes += listener.OnClearVotes;
       
        }

        public void RemovePlannerDataChangeListener(PlannerDataChangeListener listener)
        {
            OnArrangedProject -= listener.OnArrangedProject;

            OnCreatedBacklog -= listener.OnCreatedBacklog;
            OnCreatedIteration -= listener.OnCreatedIteration;
            OnCreatedProject -= listener.OnCreatedProject;
            OnCreatedStoryCard -= listener.OnCreatedStoryCard;

            OnDeletedBacklog -= listener.OnDeletedBacklog;
            OnDeletedIteration -= listener.OnDeletedIteration;
            OnDeletedProject -= listener.OnDeletedProject;
            OnDeletedStoryCard -= listener.OnDeletedStoryCard;

            OnGotProjectNames -= listener.OnGotProjectNames;
            OnLoadedProject -= listener.OnLoadedProject;

            OnMovedStoryCardToNewParent -= listener.OnMovedStoryCardToNewParent;

            OnUndeletedBacklog -= listener.OnUndeletedBacklog;
            OnUndeletedIteration -= listener.OnUndeletedIteration;
            OnUndeletedStoryCard -= listener.OnUndeletedStoryCard;

            OnUpdatedBacklog -= listener.OnUpdatedBacklog;
            OnUpdatedIteration -= listener.OnUpdatedIteration;
            OnUpdatedStoryCard -= listener.OnUpdatedStoryCard;
            OnUpdatedLegend -= listener.OnUpdatedLegend;
            OnUpdatedProject -= listener.OnUpdatedProject;

            OnAsynchronousException -= listener.OnAsynchronousException;

            OnMouseMoved -= listener.OnMouseMove;

            OnOtherClientDisconnected -= listener.OnOtherClientDisconnected;

            OnLiveUpdated -= listener.OnLiveUpdated;
            OnCardMinimized -= listener.OnCardMinimized;

            OnStartedVoting -= listener.OnStartedVoting;
            OnStoppedVoting -= listener.OnStoppedVoting;
            OnVotedCard -= listener.OnVotedCard;
            OnClearedVotes -= listener.OnClearVotes;
                      
        }

        #endregion

        #endregion

        #region Callback Methods

        public void ArrangedProject(Project project)
        {
            if (OnArrangedProject != null)
                OnArrangedProject(project);
        }

        public void CreatedBacklog(Backlog backlog)
        {
            if (OnCreatedBacklog != null)
                OnCreatedBacklog(backlog);
        }

        public void CreatedIteration(Iteration iteration, bool isCreatedByRemoteClient)
        {
            if (OnCreatedIteration != null)
                OnCreatedIteration(iteration,isCreatedByRemoteClient);
        }

        public void CreatedIteration(Iteration iteration)
        {
            this.CreatedIteration(iteration, false);
        }

        public void CreatedProject(Project project)
        {
            if (OnCreatedProject != null)
                OnCreatedProject(project);
        }

        public void CreatedStoryCard(StoryCard storycard)
        {
            if (OnCreatedStoryCard != null)
                OnCreatedStoryCard(storycard);

        }

        public void DeletedIteration(Iteration iteration)
        {
            if (OnDeletedIteration != null)
                OnDeletedIteration(iteration);
        }

        public void DeletedBacklog(Backlog backlog)
        {
            if (OnDeletedBacklog != null)
                OnDeletedBacklog(backlog);
        }

        public void DeletedProject(bool deleted)
        {
            if (OnDeletedProject != null)
                OnDeletedProject(deleted);
        }

        public void DeletedStoryCard(StoryCard storycard)
        {
            if (OnDeletedStoryCard != null)
                OnDeletedStoryCard(storycard);
        }
        
        public void GotProjectNames(List<string> projectNames)
        {
            if (OnGotProjectNames != null)
                OnGotProjectNames(projectNames);
        }

        public void LoadedProject(Project project)
        {
            if (OnLoadedProject != null)
                OnLoadedProject(project);
        }

        public void MovedStoryCardToNewParent(StoryCard storycard)
        {
            if (OnMovedStoryCardToNewParent != null)
                OnMovedStoryCardToNewParent(storycard);
        }

        public void UndeletedCard(IndexCard card)
        {
            if (card is StoryCard)
            {
                UndeletedStoryCard((StoryCard)card);
            }
            else if (card is Iteration)
            {
                UndeletedIteration((Iteration)card);
            }
        }
        public void UndeletedBacklog(Backlog backlog)
        {
            if (OnUndeletedBacklog != null)
                OnUndeletedBacklog(backlog);
        }

        public void UndeletedIteration(Iteration iteration)
        {
            if (OnUndeletedIteration != null)
                OnUndeletedIteration(iteration);
        }

        public void UndeletedStoryCard(StoryCard storycard)
        {
            if (OnUndeletedStoryCard != null)
                OnUndeletedStoryCard(storycard);
        }

        public void UpdatedBacklog(Backlog backlog)
        {
            if (OnUpdatedBacklog != null)
                OnUpdatedBacklog(backlog);
        }

        public void UpdatedIteration(Iteration iteration)
        {
            if (OnUpdatedIteration != null)
                OnUpdatedIteration(iteration);
        }

        public void UpdatedLegend(Legend legend)
        {
            if (OnUpdatedLegend != null)
                OnUpdatedLegend(legend);
        }

        public void UpdatedProject(Project project)
        {
            if (OnUpdatedLegend != null)
                OnUpdatedProject(project);
        }
        
        public void UpdatedStoryCard(StoryCard storycard)
        {
            if (OnUpdatedStoryCard != null)
                OnUpdatedStoryCard(storycard);
        }
        
        public void AsynchronousException(Exception exception)
        {
            if (OnAsynchronousException != null)
                OnAsynchronousException(exception);
        }

        public void MouseMoved(MouseMove move)
        {
            if (OnMouseMoved != null)
                OnMouseMoved(move);
        }

        public void OtherClientDisconnected(int clientID)
        {
            if (OnOtherClientDisconnected != null)
                OnOtherClientDisconnected(clientID);

        }

        public void LiveUpdated(LiveUpdate update)
        {
            if (OnLiveUpdated != null)
                OnLiveUpdated(update);
        }

        public void CardMinimized(long ID,bool isMinimized)
        {
            if(OnCardMinimized != null)
              OnCardMinimized(ID,isMinimized);
        }

        #endregion

        #region Dot Voting

        public void StartedVoting()
        {
            if (OnStartedVoting != null)
                OnStartedVoting();
        }

        public void VotedCard(VoteDot dot)
        {
            if (OnVotedCard != null)
                OnVotedCard(dot);
        }

        public void StoppedVoting()
        {
            if (OnStoppedVoting != null)
                OnStoppedVoting();
        }

        public void ClearedVotes()
        {
            if (OnClearedVotes != null)
                OnClearedVotes();
        }

        #endregion

        #region AsynchronousPersister Members




        #endregion
    }
}
